package red.dao.impl;

import red.dao.ArticleDao;
import red.model.ArticleModel;
import red.util.MysqlConnectUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ArticleDaoImpl implements ArticleDao {

    private MysqlConnectUtils mysqlconnectutils = new MysqlConnectUtils();

//    删除文章标题
    @Override
    public ArticleModel deleteArticle(ArticleModel articleModel) {
        String headline = articleModel.getHeadline();
        String sql = "delete from article where headline= "
                + "'"+headline+ "'";
        mysqlconnectutils.delete(sql);
        return articleModel;
    }

//    查询文章<标题查询><栏目><人名>
    @Override
    public List<ArticleModel> selectArticle(ArticleModel articleModel) {
        String headline = articleModel.getHeadline();
        String sql = "select * from article where headline= "
                + "'"+headline+ "'";
        List<ArticleModel> models =new ArrayList<>();
        List<Map<String, Object>> list = mysqlconnectutils.list(sql);
        for(int i=0;i<list.size();i++){
            Map<String, Object> row = list.get(i);
            ArticleModel model = new ArticleModel();
            model.setArticle_id(row.get("article_id").toString());
            model.setUser_name(row.get("user_name").toString());
            model.setHeadline(row.get("headline").toString());
            model.setColumn_name(row.get("column_name").toString());
            model.setContent(row.get("content").toString());
           model.setPage(row.get("page").toString());
           model.setLike_number(row.get("like_number").toString());
            model.setAudit(row.get("audit").toString());
            model.setPublish_date(row.get("publish_date").toString());
            model.setColumn_id(row.get("column_id").toString());
            model.setUser_id(row.get("user_id").toString());
            models.add(model);
        }
        return models;
    }

    @Override
    public List<ArticleModel> selectArticleColumn(ArticleModel articleModel) {
        String column_name = articleModel.getColumn_name();
        String sql = "select * from article where column_name= "
                + "'"+column_name+ "'";
        List<ArticleModel> models =new ArrayList<>();
        List<Map<String, Object>> list = mysqlconnectutils.list(sql);
        for(int i=0;i<list.size();i++){
            Map<String, Object> row = list.get(i);
            ArticleModel model = new ArticleModel();
            model.setArticle_id(row.get("article_id").toString());
            model.setUser_name(row.get("user_name").toString());
            model.setHeadline(row.get("headline").toString());
            model.setColumn_name(row.get("column_name").toString());
            model.setContent(row.get("content").toString());
            model.setPage(row.get("page").toString());
            model.setLike_number(row.get("like_number").toString());
            model.setAudit(row.get("audit").toString());
            model.setPublish_date(row.get("publish_date").toString());
            model.setColumn_id(row.get("column_id").toString());
            model.setUser_id(row.get("user_id").toString());
            models.add(model);
        }
        return models;
    }

    @Override
    public List<ArticleModel> selectArticleUser(ArticleModel articleModel) {
        String user_name = articleModel.getUser_name();
        String sql = "select * from article where user_name= "
                + "'"+user_name+ "'";
        List<ArticleModel> models =new ArrayList<>();
        List<Map<String, Object>> list = mysqlconnectutils.list(sql);
        for(int i=0;i<list.size();i++){
            Map<String, Object> row = list.get(i);
            ArticleModel model = new ArticleModel();
            model.setArticle_id(row.get("article_id").toString());
            model.setUser_name(row.get("user_name").toString());
            model.setHeadline(row.get("headline").toString());
            model.setColumn_name(row.get("column_name").toString());
            model.setContent(row.get("content").toString());
            model.setPage(row.get("page").toString());
            model.setLike_number(row.get("like_number").toString());
            model.setAudit(row.get("audit").toString());
            model.setPublish_date(row.get("publish_date").toString());
            model.setColumn_id(row.get("column_id").toString());
            model.setUser_id(row.get("user_id").toString());
            models.add(model);
        }
        return models;
    }

//查询所有文章
    @Override
    public List<ArticleModel> selectq(ArticleModel articleModel) {
        String sql = "select * from article ";
        List<ArticleModel> models =new ArrayList<>();
        List<Map<String, Object>> list = mysqlconnectutils.list(sql);
        for(int i=0;i<list.size();i++){
            Map<String, Object> row = list.get(i);
            ArticleModel model = new ArticleModel();
            model.setArticle_id(row.get("article_id").toString());
            model.setUser_name(row.get("user_name").toString());
            model.setHeadline(row.get("headline").toString());
            model.setColumn_name(row.get("column_name").toString());
            model.setContent(row.get("content").toString());
            model.setPage(row.get("page").toString());
            model.setLike_number(row.get("like_number").toString());
            model.setAudit(row.get("audit").toString());
            model.setPublish_date(row.get("publish_date").toString());
            model.setColumn_id(row.get("column_id").toString());
            model.setUser_id(row.get("user_id").toString());
            models.add(model);
        }
        return models;
    }


}
